from xml.dom import minidom
import datetime

from .call_service_base import *
from .parse_service_base import *
from .. import tables

'''
'''
class CallService(CallServiceBase):

    def __init__(self, c, domain):
        '''
        '''
        self.control = c
        self.domain = domain
        self.parser = ParseService()
        self.url_path = '/maps/tpws/V1/tripplanner/appID/8EB2B259743166EF7569C6C78/format/xml/?'
        self.urls = None

class ParseService(ParseServiceBase):

    def __init__(self):
        super(ParseService, self).__init__()

    def parse(self, doc, url, url_rec):
        trip = super(ParseService, self).parse(doc, url, url_rec)

        # step 1: parse the xml document
        trip.trip_planner = 'ATIS'
        dom = minidom.parseString(doc)

        # step 2: find high-level elements in dom
        dom_from = dom.getElementsByTagName('from')[0]
        dom_to = dom.getElementsByTagName('to')[0]
        dom_request = dom.getElementsByTagName('request')[0]
        dom_itineraries = dom.getElementsByTagName('itinerary')

        # step 3: parse from & to parameters (set to self, for call to self.makeItinerary() below)
        trip.from_lat, trip.from_lon = ParseService.xml_coord(dom_from)
        trip.from_name = ParseService.xml_value(dom_from, 'description')
        trip.to_lat, trip.to_lon = ParseService.xml_coord(dom_to)
        trip.to_name = ParseService.xml_value(dom_to, 'description')

        #step 4: parse out the trip options
        trip.optimize_flag = ParseService.xml_attribute(dom_request, 'param', 'Min')
        trip.arrive_flag = ParseService.xml_attribute(dom_request, 'param', 'Arr')
        trip.walk_distance = ParseService.xml_attribute(dom_request, 'param', 'Walk')
        trip.mode = ParseService.xml_attribute(dom_request, 'param', 'Mode')

        d = ParseService.xml_attribute(dom_request, 'param', 'date')
        t = ParseService.xml_attribute(dom_request, 'param', 'time')
        trip.date_time = ParseService.make_datetime(d, t)

        # step 5: parse out the data for the the itinerary object(s)
        self.parse_itineraries(trip, dom_itineraries)
        trip.message = ParseService.xml_value(dom, 'message')
        self.trips.append(trip)
        return trip


    def parse_itinerary(self, trip, dom):
        ''' parse out things like start & end time, duration, transit times, walking and waiting times, etc...

          <time-distance>
            <date>6/01/11</date>
            <startTime>11:42 AM</startTime>
            <endTime>12:34 PM</endTime>
            <duration>55</duration>
            <distance>8.09</distance>
            <numberOfTransfers>0</numberOfTransfers>
            <numberOfTripLegs>2</numberOfTripLegs>
            <walkingTime>3</walkingTime>
            <transitTime>52</transitTime>
        <time-distance>
        '''
        td = dom.getElementsByTagName('time-distance')[0]
        itin = self.make_itinerary(trip)

        d = ParseService.xml_value(td, 'date')
        st = ParseService.xml_value(td, 'startTime')
        et = ParseService.xml_value(td, 'endTime')
        itin.start_time = ParseService.make_datetime(d, st, '%m/%d/%y')
        itin.end_time = ParseService.make_datetime(d, et, '%m/%d/%y')

        itin.walk_time = ParseService.xml_value(td, 'walkingTime')
        itin.transit_time = ParseService.xml_value(td, 'transitTime')
        itin.duration = ParseService.xml_value(td, 'duration')
        itin.wait_time = int(itin.duration) - (int(itin.transit_time) + int(itin.walk_time))
        itin.transfers = ParseService.xml_value(td, 'numberOfTransfers')
        itin.distance = ParseService.xml_value(td, 'distance')
        return itin

